[?php

/**
 * <?php echo $this->getModuleName() ?> module configuration.
 *
 * @package    ##PROJECT_NAME##
 * @subpackage <?php echo $this->getModuleName()."\n" ?>
 * @author     ##AUTHOR_NAME##
 * @version    SVN: $Id: helper.php 14891 2009-01-20 06:47:03Z dwhittle $
 */
class Base<?php echo ucfirst($this->getModuleName()) ?>GeneratorHelper extends sfModelGeneratorHelper
{
  public function getGoBackUrl($default=null)
  {
    $user = sfContext::getInstance()->getUser();
    if (!is_null($user) && (method_exists($user, 'getGoBackTo')))
    {
      return $user->getGoBackTo('sf_admin', $default);
    }
    return $default;
  }

  public function fillSlotActionDefaults(&$params)
  {
    $params['label'] = (isset($params['label'])) ? $params['label'] : sfInflector::humanize($params['action']);
    $params['icon'] = (isset($params['icon'])) ? $params['icon'] : '<?php echo $this->getDefaultValue('slot.icon') ?>';
    $params['params']['title']   = __($params['label'], array(), 'sf_admin');
    $params['params']['class'] = (isset($params['params']['class'])) ? $params['params']['class'] : '<?php echo $this->getDefaultValue('slot.class') ?>';
    $params['params']['image_class'] = (isset($params['params']['image_class'])) ? $params['params']['image_class'] : '<?php echo $this->getDefaultValue('slot.image_class') ?>';
    $params['params']['text_class'] = (isset($params['params']['text_class'])) ? $params['params']['text_class'] : '<?php echo $this->getDefaultValue('slot.text_class') ?>';
    if (isset($params['query_string']))
    {
      $params['params']['query_string'] = $params['query_string'];
      unset($params['query_string']);
    }
    $params['confirm'] = (isset($params['confirm']) ? $params['confirm'] : '');
  }

  public function getCurtainJavascript($params=array())
  {
    $js = '';
    if ((!isset($params['confirm'])) || (empty($params['confirm'])))
      $js = "document.body.appendChild(new Element('div', { 'style' :  'background: #fff none; height: 100%; width: 100%; position: absolute; top: 0; left: 0; filter: alpha(opacity=20); opacity: 0.2;' }));";
    return $js;
  }

  public function getSlotActionImage($params, $default = '<?php echo $this->getDefaultValue('slot.icon') ?>')
  {
    if (!isset($params['icon']) && is_null($default))
      return '';
    if (!isset($params['params']['title'])) $params['params']['title'] = __($params['label'], array(), 'sf_admin');

    return image_tag((isset($params['icon']) ? $params['icon'] : $default), array('alt_title' => $params['params']['title'], 'class' => $params['params']['image_class'])) . ' ';
  }

  public function getSlotActionText($params)
  {
    return '<span class="' . $params['params']['text_class'] . '">' . __($params['label'], array(), 'sf_admin') . '</span>';
  }

  public function slotActionToNew($params)
  {
    $params['action'] = 'new';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/create.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), '@'.$this->getUrlForAction('new'), $params['params']);
  }

  public function slotActionToExportation($params)
  {
    $params['action'] = isset($params['action'])? $params['action'] : 'doExportationPages';
    $params['label'] = 'Export';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/spreadsheet.png');
    $this->fillSlotActionDefaults($params);

    return link_to_remote($this->getSlotActionImage($params) . $this->getSlotActionText($params),
                          array('url'     => sfContext::getInstance()->getModuleName().'/'.$params['action'],
                                'update'  => 'sf_admin_exportation_form',
                                'script'  => true,
                                'loading'  => "jQuery('#sf_admin_exportation').show(); jQuery('#sf_admin_exportation_ajax_indicator').show(); jQuery('#sf_admin_exportation_form').hide(); jQuery('#sf_admin_exportation').centerHorizontally(); jQuery('#sf_admin_exportation_resizable_area').ensureVisibleHeight();",
                                'complete' => "jQuery('#sf_admin_exportation').show(); jQuery('#sf_admin_exportation_ajax_indicator').hide(); jQuery('#sf_admin_exportation_form').show(); jQuery('#sf_admin_exportation').centerHorizontally(); jQuery('#sf_admin_exportation_resizable_area').ensureVisibleHeight();",
                                ),
                          $params['params']);
  }

  public function slotActionToUserExportation($params)
  {
    $params['action'] = isset($params['action'])? $params['action'] : 'newUserExportation';
    $params['label'] = 'Custom export';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/spreadsheet.png');
    $this->fillSlotActionDefaults($params);

    return link_to_remote($this->getSlotActionImage($params) . $this->getSlotActionText($params),
                          array('url'     => sfContext::getInstance()->getModuleName().'/'.$params['action'],
                                'update'  => 'sf_admin_exportation_form',
                                'script'  => true,
                                'loading'  => "jQuery('#sf_admin_exportation').show(); jQuery('#sf_admin_exportation_ajax_indicator').show(); jQuery('#sf_admin_exportation_form').hide(); jQuery('#sf_admin_exportation').centerHorizontally(); jQuery('#sf_admin_exportation_resizable_area').ensureVisibleHeight();",
                                'complete' => "jQuery('#sf_admin_exportation').show(); jQuery('#sf_admin_exportation_ajax_indicator').hide(); jQuery('#sf_admin_exportation_form').show(); jQuery('#sf_admin_exportation').centerHorizontally(); jQuery('#sf_admin_exportation_resizable_area').ensureVisibleHeight();",
                                ),
                          $params['params']);
  }

  public function slotActionToClose($params)
  {
    $params['action'] = 'close';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/close.png');
    $this->fillSlotActionDefaults($params);

    return link_to_function($this->getSlotActionImage($params) . $this->getSlotActionText($params), "window.close();", $params['params']);
  }

  public function slotActionTo($route_or_url, $params)
  {
    $params['action'] = 'goTo'.sfInflector::classify($route_or_url);
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), $route_or_url, $params['params']);
  }

  public function slotActionToFunctionToRemote($function_to_remote_args, $params)
  {
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return link_to_remote($this->getSlotActionImage($params) . $this->getSlotActionText($params), $function_to_remote_args, $params['params']);
  }

  public function slotActionToFunction($link_to_function_args, $params)
  {
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return link_to_function($this->getSlotActionImage($params) . $this->getSlotActionText($params), $link_to_function_args, $params['params']);
  }

  public function slotActionToCartAdd($params)
  {
    $params['action'] = 'Add to cart';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/cart_add.png');
    $this->fillSlotActionDefaults($params);

    return link_to_function($this->getSlotActionImage($params) . $this->getSlotActionText($params), $this->getCurtainJavascript($params) . $this->getJSBatchFunction('batchAddToCart'), $params['params']);
  }

  public function slotActionToCartAddAll($params)
  {
    $params['label'] = 'Add all to cart';
    $params['action'] = 'cartAddAll';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/cart_add_all.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/'.$params['action'], $params['params']);
  }

  public function slotActionToCartFormAction($params)
  {
    $params['action'] = 'cartFormAction?form_class='.$params['form'].(isset($params['condition'])?'&batch_c='.$params['condition']:'');
    $params['icon']   = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/'.$params['action'], $params['params']);
  }

  public function slotActionToCartBatchFormAction($params)
  {
    $params['action'] = 'cartBatchFormAction?form_class='.$params['form'].(isset($params['condition'])?'&batch_c='.$params['condition']:'');
    $params['icon']   = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return '<span class="batch_button">'.link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/'.$params['action'], $params['params'])."</span>";
  }

  public function slotActionToCartBatchAction($params)
  {
    $params['action'] = 'cartBatchAction?batch_action='.$params['batch_action'].(isset($params['condition'])?'&batch_c='.$params['condition']:'');
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return '<span class="batch_button">'.link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/'.$params['action'], $params['params']).'</span>';
  }

  protected function getCartRedirectUrl($params)
  {
    if (isset($params['redirect']))
    {
      return $params['redirect'];
    }
    elseif (isset($params['action']))
    {
      return sfContext::getInstance()->getModuleName().'/'.$params['action'];
    }
  }

  public function slotActionToCartRedirectAction($params)
  {
    $params['action'] = 'cartRedirectAction?redirect='.$this->getCartRedirectUrl($params).'&module_name='.sfContext::getInstance()->getModuleName().(isset($params['condition'])?'&batch_c='.$params['condition']:'');
    $params['icon']   = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/generic.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/'.$params['action'], $params['params']);
  }

  public function slotActionToCartManipulate($params=array())
  {
    $params['action'] = 'Manipulate items in cart';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/cart_manipulate.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/cartIndex', $params['params']);
  }

  public function slotActionToHomepage($params=array())
  {
    $params['action'] = 'Homepage';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/home.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), "@homepage", $params['params']);
  }

  public function slotActionToEdit($object, $params)
  {
    $params['action'] = 'edit';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/edit.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), $this->getUrlForAction('edit'), $object, $params['params']);
  }

  public function slotActionToDelete($object, $params)
  {
    if ($object->isNew())
    {
      return '';
    }

    $params['action'] = 'delete';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/delete.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), $this->getUrlForAction('delete'), $object, array_merge($params['params'], array('method' => 'delete', 'confirm' => !empty($params['confirm']) ? __($params['confirm'], array(), 'sf_admin') : __('Are you sure you want to delete the object?', array(), 'sf_admin'))));
  }

  public function slotActionToList($params)
  {
    $params['action'] = 'list';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/list.png');
    $this->fillSlotActionDefaults($params);

    $url = $this->getGoBackUrl($this->getUrlForAction('list'));
    if (!preg_match('/^@/', $url) && false === strpos($url, '/'))
    {
      $url = '@'.$url;
    }

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), $url, $params['params']);
  }

  public function slotActionToSave($object, $params)
  {
    $params['action'] = 'save';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/save.png');
    $this->fillSlotActionDefaults($params);

    $params['params']['class'] = $params['params']['class'].' save';

    return link_to_function($this->getSlotActionImage($params) . $this->getSlotActionText($params), $this->getCurtainJavascript($params) . $this->getJSSaveFunction('_save'), $params['params']);
  }

  public function slotActionToSaveAndAdd($object, $params)
  {
    $params['action'] = 'save_and_add';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/save_and_add.png');
    $this->fillSlotActionDefaults($params);

    $params['params']['class'] = $params['params']['class'].' save';

    return link_to_function($this->getSlotActionImage($params) . $this->getSlotActionText($params), $this->getCurtainJavascript($params) . $this->getJSSaveAndListHackFunction('_save_and_add') . $this->getJSSaveFunction('_save_and_add'), $params['params']);
  }
  
  public function slotActionToSaveAndList($object, $params)
  {
    $params['action'] = 'save_and_list';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/save_and_add.png');
    $this->fillSlotActionDefaults($params);

    $params['params']['class'] = $params['params']['class'].' save';

    return link_to_function($this->getSlotActionImage($params) . $this->getSlotActionText($params), $this->getCurtainJavascript($params). $this->getJSSaveAndListHackFunction('_save_and_list') . $this->getJSSaveFunction('_save_and_list'), $params['params']);
  }

  public function slotActionToCartReset($params)
  {
    $params['action'] = 'cart_reset';
    $params['icon'] = (isset($params['icon']) ? $params['icon'] : '/sfPropelRevisitedGeneratorPlugin/images/icons/24x24/cart_reset.png');
    $this->fillSlotActionDefaults($params);

    return link_to($this->getSlotActionImage($params) . $this->getSlotActionText($params), sfContext::getInstance()->getModuleName().'/cartReset', $params['params']);
  }

  public function getJSBatchFunction($action)
  {
    return "var form = document.getElementById('sf_admin_list_form'); var submit = document.getElementById('batch_action'); submit.value='$action'; form.submit();";
  }

  public function getJSSaveFunction($action)
  {
    return "var form = document.getElementById('sf_admin_edit_form'); var submit_value = document.createElement('input'); submit_value.style.display = 'none'; submit_value.type='submit'; submit_value.name='$action'; submit_value.value='$action'; form.appendChild(submit_value); submit_value.click();";
  }

  public function getJSSaveAndListHackFunction($action)
  {
    return "var form = document.getElementById('sf_admin_edit_form'); var submit_value = document.createElement('input'); submit_value.style.display = 'none'; submit_value.type='text'; submit_value.name='$action'; submit_value.value='$action'; form.appendChild(submit_value);";
  }

  public function linkToNew($params)
  {
    return '<li class="sf_admin_action_new">'.link_to(__($params['label'], array(), 'sf_admin'), $this->getUrlForAction('new')).'</li>';
  }

  public function linkToEdit($object, $params)
  {
    return '<li class="sf_admin_action_edit">'.link_to(__($params['label'], array(), 'sf_admin'), $this->getUrlForAction('edit'), $object).'</li>';
  }

  public function linkTo($object, $action, $params)
  {
    $url = '<?php echo $this->params['route_prefix'] ?>/'.$action;
    if (!isset($params['params']) || !is_array($params['params'])) $params['params'] = array('class' => 'sf_admin_action_custom');
    if (!isset($params['params']['class'])) $params['params']['class'] = 'sf_admin_action_custom';

    return '<li class="'.$params['params']['class'].'">'.link_to(__($params['label'], array(), 'sf_admin'), $url, $object).'</li>';
  }

  public function linkToShow($object, $params)
  {
    return '<li class="sf_admin_action_show">'.link_to(__($params['label'], array(), 'sf_admin'), $this->getUrlForAction('show'), $object).'</li>';
  }

  public function linkToCartDelete($object, $params)
  {
    if ($object->isNew())
    {
      return '';
    }
    $options = array('method' => 'cart_delete');
    if (!isset($params['confirm']))
    {
      $options['onclick'] = $this->getCurtainJavascript();
    }
    else
    {
      $options['confirm'] = __($params['confirm'], array(), 'sf_admin');
    }

    return '<li class="sf_admin_action_cart_delete">'.link_to(__($params['label'], array(), 'sf_admin'), sfContext::getInstance()->getModuleName().'/cartDelete?id='.$object->getPrimaryKey()).'</li>';
  }


  public function linkToDelete($object, $params)
  {
    if ($object->isNew())
    {
      return '';
    }
    $options = array('method' => 'delete');
    if (!isset($params['confirm']))
    {
      $options['onclick'] = $this->getCurtainJavascript();
    }
    else
    {
      $options['confirm'] = __($params['confirm'], array(), 'sf_admin');
    }

    return '<li class="sf_admin_action_delete">'.link_to(__($params['label'], array(), 'sf_admin'), $this->getUrlForAction('delete'), $object, $options).'</li>';
  }

  public function linkToList($params)
  {
    return '<li class="sf_admin_action_list">'.link_to(__($params['label'], array(), 'sf_admin'), $this->getGoBackUrl($this->getUrlForAction('list'))).'</li>';
  }

  public function linkToSave($object, $params)
  {
    return '<li class="sf_admin_action_save"><input type="submit" value="'.__($params['label'], array(), 'sf_admin').'" onclick="'.$this->getCurtainJavascript($params).'" /></li>';
  }

  public function linkToSaveAndAdd($object, $params)
  {
    return '<li class="sf_admin_action_save_and_add"><input type="submit" value="'.__($params['label'], array(), 'sf_admin').'" onclick="'.$this->getCurtainJavascript($params).'" name="_save_and_add" /></li>';
  }
  
  public function linkToSaveAndList($object, $params)
  {
    return '<li class="sf_admin_action_save_and_list"><input type="submit" value="'.__($params['label'], array(), 'sf_admin').'" onclick="'.$this->getCurtainJavascript($params).'" name="_save_and_list" /></li>';
  }

  public function getUrlForAction($action)
  {
    return 'list' == $action ? '<?php echo $this->params["route_prefix"] ?>' : '<?php echo $this->params["route_prefix"] ?>_'.$action;
  }
}
